package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.util.DateAndTimeUtil;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guofen.service.shlj.YijingStationSellingService;
import com.guofen.dao.ShljYijingStationSellingMapper;
import com.guofen.pojo.ShljYijingStationSelling;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class YijingStationSellingServiceImpl implements YijingStationSellingService{
    @Autowired
    private ShljYijingStationSellingMapper shljYijingStationSellingMapper;

    @Override
    public boolean add(String name,
                       Double amount,
                       String remarks){
        String date = DateAndTimeUtil.getDate();

        ShljYijingStationSelling shljYijingStationSelling = new ShljYijingStationSelling();
        shljYijingStationSelling.setName(name);
        shljYijingStationSelling.setDate(date);
        shljYijingStationSelling.setAmount(amount);
        shljYijingStationSelling.setUnit("公斤");
        Double price = 1.23;//临时给一个数
        shljYijingStationSelling.setPrice(price);
        shljYijingStationSelling.setRemarks(remarks);
        shljYijingStationSelling.setTotalPrice(amount*price);
        int result = shljYijingStationSellingMapper.insert(shljYijingStationSelling);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public ShljYijingStationSelling getlast(){
        return shljYijingStationSellingMapper.getLast();
    }

    @Override
    public List<ShljYijingStationSelling> getrecord_day(){
        return shljYijingStationSellingMapper.getrecord_day();
    }

    @Override
    public List<Double> getamount_day(){
        List<Double> result = new ArrayList();
        List<String> garbage = Arrays.asList("塑料", "塑料袋", "饭盒", "纸", "铁铝盒", "玻璃瓶", "饮料瓶");
        List<ShljYijingStationSelling> itemlist = getrecord_day();
        for (String item:garbage) {
            Double amount = 0.0;
            for(ShljYijingStationSelling temp:itemlist){
                if(temp.getName().equals(item)){
                    amount+=temp.getAmount();
                }
            }
            result.add(amount);
        }
        return result;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljYijingStationSelling> list = shljYijingStationSellingMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljYijingStationSelling> list = shljYijingStationSellingMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljYijingStationSellingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljYijingStationSelling record) {
        return shljYijingStationSellingMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public boolean addFormExcel(String date, String name, Double amount, String unit, Double price, Double totalPrice, String remarks) {
        ShljYijingStationSelling shljYijingStationSelling = new ShljYijingStationSelling();
        shljYijingStationSelling.setName(name);
        shljYijingStationSelling.setDate(date);
        shljYijingStationSelling.setAmount(amount);
        shljYijingStationSelling.setUnit(unit);
        shljYijingStationSelling.setPrice(price);
        shljYijingStationSelling.setRemarks(remarks);
        shljYijingStationSelling.setTotalPrice(totalPrice);
        int result = shljYijingStationSellingMapper.insert(shljYijingStationSelling);
        if (result == 1)
            return true;
        else
            return false;
    }
}